package com.h3bpm.handler;

import OThinker.Common.Organization.Models.User;
import OThinker.H3.Entity.IEngine;
import OThinker.H3.Entity.Instance.InstanceContext;
import OThinker.H3.Entity.WorkItem.WorkItemModels.WorkItem;
import OThinker.H3.Entity.WorkItem.WorkItemState;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.HashMap;

public class UserProtalHelper {
    private static final Logger logger = LoggerFactory.getLogger(UserProtalHelper.class);
    //正式
    private static String Content_Type="application/json; charset=utf-8";

    private static String grant_type="password";

    private static String username="ZJALB1002001";

    private static String password="zjlabbpm123!@#";

    private static String method="POST";

    private static Integer ONE=1;
    private static Integer TWO=2;
    private static Integer There=3;
    private static Integer FOUR=4;
    private static Integer FIVE=5;
    private static Integer SIX=6;
    private static Integer ZERO=0;

    private static Integer TIMES=1000;

    /*
     * 创建实例
     * */
    public static boolean createInstance(IEngine engine, InstanceContext instanceContext) {
        try {
            Boolean flag = OrcaleSelectHelper.selectProtalInstanceId(instanceContext.getInstanceId(),engine);
            //不存在添加
            if(flag){
                String instancId= instanceContext.getInstanceId();
                String originator = instanceContext.getOriginator();

                String originatorName = instanceContext.getOriginatorName();
                String bizObjectSchemaCode = instanceContext.getBizObjectSchemaCode();
                Date time = instanceContext.getCreatedTime();
                String instanceName = instanceContext.getInstanceName();
                String bizObjectId = instanceContext.getBizObjectId();
                Boolean flag2=  OrcaleSelectHelper.storageProtalInstanceId(instancId,instanceName,originatorName,bizObjectId,bizObjectSchemaCode,originator,engine);
                if(flag2){
                    logger.info("创建成功===============instanceIdId"+instanceContext.getInstanceId());
                }else {
                    logger.info("创建失败===============instanceIdId"+instanceContext.getInstanceId());
                }
            }else {
                logger.info("该实例已存在===============instanceIdId"+instanceContext.getInstanceId());
            }


        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
        /*
          更新实例状态

         */
    public static boolean updateInstance(IEngine engine, InstanceContext instanceContext) {
        try {
            logger.info("更新实例结果====================instanceIdId"+instanceContext.getInstanceId());
            String instanceId=instanceContext.getInstanceId();
            String instanceState=String.valueOf(instanceContext.getState().getValue());
            Boolean requet= OrcaleSelectHelper.createProtalUpdateInstance(engine,instanceId,instanceState);
            if(requet){
                logger.info("更新实例结果====================success");
                return true;
            }else {
                logger.info("更新实例结果====================失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    /*
     * 存储代办
     * */
    public static boolean createWorkItem(IEngine engine, WorkItem item) {
        try{
            InstanceContext instanceContext = engine.getInstanceManager().GetInstanceContext(item.getInstanceID());
            String receiver = ((User) engine.getOrganization().GetUnit(item.getParticipant())).getCode(); // 接收人（原值）
            String schemaCode =   instanceContext.getBizObjectSchemaCode();
            String forwarderName = item.getForwarderName();
            String displayName=item.getDisplayName();
            String participant=item.getParticipant();
            HashMap<String, Object> body = new HashMap<>();
            body.put("instance",instanceContext.getInstanceId());
            body.put("taskId",item.getObjectID());
            body.put("displayName",displayName);
            body.put("participantId",receiver);
            body.put("participant",participant);
            body.put("url",UserHelper.pcUrl(schemaCode,item.getObjectID()));
            body.put("h5url",UserHelper.h5Url(schemaCode,item.getObjectID()));
            Boolean requestString= OrcaleSelectHelper.storageProtalTask(engine,body);
            if(requestString){
                logger.info("存储代办结果====================成功");
                return true;
            }else {
                logger.info("创建代办结果====================失败");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
}
